package gameHandling;

import main.PlayingCanvas;
import main.Res;

public class PathFinder {

	static int[][] flag = new int[8][20];
	static int[] points = new int[160];
	static int[] dirI = new int[] { 0, -1, 1, 0, 1, -1, -1, 1 };
	static int[] dirJ = new int[] { 1, 0, 0, -1, 1, 1, -1, -1 };
	static int startIndex;
	static int endIndex;

	static public boolean isLine(int si, int sj, int ti, int tj) {
		double x = ti - si;
		double y = tj - sj;
		double stepX, stepY;
		int step;
		if (Math.abs(x) > Math.abs(y)) {
			if (x > 0) {
				stepX = 1;
			} else {
				stepX = -1;
			}
			step = (int) Math.abs(x) - 1;
			stepY = stepX * y / x;
		} else {
			if (y > 0) {
				stepY = 1;
			} else {
				stepY = -1;
			}
			stepX = stepY * x / y;
			step = (int) Math.abs(y) - 1;
		}
		x = si + 0.5;
		y = sj + 0.5;
		for (int i = 0; i < step; i++) {
			x += stepX;
			y += stepY;
			if (PlayingCanvas.getGame().objectMap[(int) x][(int) y] == 2) {
				return false;
			}
		}
		return true;
	}

	static public int findPath(int si, int sj, int ti, int tj, int depth) {

		for (int i = 0; i < 8; i++) {
			for (int j = 0; j < 20; j++) {
				if (PlayingCanvas.getGame().objectMap[i][j] == 2) {
					flag[i][j] = -9;
				} else {
					flag[i][j] = -1;
				}
			}
		}
		startIndex = 0;
		endIndex = 1;
		points[0] = si + sj * 8;
		flag[si][sj] = -5;
		while (true) {
			int ci = points[startIndex] % 8;
			int cj = points[startIndex] / 8;
			for (int i = 0; i < 4; i++) {
				int ni = ci + dirI[i];
				int nj = cj + dirJ[i];
				if (ni == ti && nj == tj) {

					int index = 0;
					int bi = ci;
					int bj = cj;

					while (index != -5) {
						index = flag[bi][bj];
						if (index == points[0]) {
							return bi + bj * 8;
						}
						bi = index % 8;
						bj = index / 8;
					}
				}
				if (ni >= 0 && nj >= 0 && ni < 8 && nj < 20) {
					if (flag[ni][nj] == -1) {
						points[endIndex++] = ni + nj * 8;
						flag[ni][nj] = points[startIndex];
					}
				}
			}
			startIndex++;
			if (startIndex == endIndex) {
				return -1;
			}
		}
	}
	static public void getFenceConnect(int si, int sj,int[][] map,int flagIndex){
		int nC = PlayingCanvas.getGame().tiledMap.getColumns();
		int nR = PlayingCanvas.getGame().tiledMap.getRows();
		for (int i = 0; i < nC; i++) {
			for (int j = 0; j < nR; j++) {
				if (PlayingCanvas.getGame().objectMap[i][j] == 2) {
					flag[i][j] = -9;
				} else {
					flag[i][j] = -1;
				}
			}
		}
		startIndex = 0;
		endIndex = 1;
		points[0] = si + sj * nC;
		flag[si][sj] = -5;
		map[si][sj] = flagIndex;
		while (true) {
			int ci = points[startIndex] % nC;
			int cj = points[startIndex] / nC;
			for (int i = 0; i < 8; i++) {
				int ni = ci + dirI[i];
				int nj = cj + dirJ[i];
				
				if (ni >= 0 && nj >= 0 && ni < nC && nj < nR
						&& flag[ni][nj] == -9) {
					points[endIndex++] = ni + nj * nC;
					flag[ni][nj] = points[startIndex];
					map[ni][nj] = flagIndex;
				}
			}
			startIndex++;
			if (startIndex == endIndex) {
				return ;
			}
		}
	}
	static public int findMonsterPath(int si, int sj) {
		int nC = PlayingCanvas.getGame().tiledMap.getColumns();
		int nR = PlayingCanvas.getGame().tiledMap.getRows();
		int []dirs = new int[]{0,1,2,3,4,5,6,7};
		for(int i=0;i<16;i++){
			int index1 = Res.getRandom(8);
			int index2 = Res.getRandom(8);
			int temp = dirs[index1];
			dirs[index1] = dirs[index2];
			dirs[index2] = temp;
		}
		for (int i = 0; i < nC; i++) {
			for (int j = 0; j < nR; j++) {
				if (PlayingCanvas.getGame().objectMap[i][j] == 2) {
					flag[i][j] = -9;
				} else {
					flag[i][j] = -1;
				}
			}
		}
		startIndex = 0;
		endIndex = 1;
		points[0] = si + sj * nC;
		flag[si][sj] = -5;
		while (true) {
			int ci = points[startIndex] % nC;
			int cj = points[startIndex] / nC;
			for (int i = 0; i < 8; i++) {
				int ni = ci + dirI[dirs[i]];
				int nj = cj + dirJ[dirs[i]];
				if (nj >= nR) {

					int index = 0;
					int bi = ci;
					int bj = cj;

					while (index != -5) {

						index = flag[bi][bj];
						if (index == points[0]) {
							return bi + bj * nC;
						}
						bi = index % nC;
						bj = index / nC;
					}
				}
				if (ni >= 0 && nj >= 0 && ni < nC && nj < nR
						&& flag[ni][nj] == -1) {
					points[endIndex++] = ni + nj * nC;
					flag[ni][nj] = points[startIndex];
				}
			}
			startIndex++;
			if (startIndex == endIndex) {
				return -1;
			}
		}
	}
}
